#include <iostream>
#include <fstream>
#include <cstring>
#include "typeinfo.h"
#include <vector>

using namespace std;

int main() {
    // 操作文本文件
    ofstream file("test_out2.txt",std::ios::app);//open 打开方式默认是std::ios::out|std::ios::trunc ，std::ios::out 表示打开文件用于写入，如果文件不存在则创建新文件，如果文件已经存在则清空文件内容。std::ios::trunc 表示截断文件，如果文件已经存在则清空文件内容。


    // 检查文件是否成功打开
    if (!file.is_open()) {
        cout << "无法打开文件" << endl;
        return 1;
    }
    file<<"name2"<<endl;
    file<<"name3"<<endl;
    file.close();
    // 读取文件内容并输出

    ifstream outFile("test_out2.txt");
    string line;
    while (getline(outFile, line))

    {
     cout<<line<<endl;
    }
    outFile.close();
    cout<<"-------use fstream----"<<endl;
    fstream outFile2("test_out2.txt",ios::in);
    string line2;
    while (getline(outFile2, line2))

    {
     cout<<line2<<endl;
    }
    outFile2.close();

    //操作二进制文件
    char buff[50];
    int buff2[55];
    fstream bfile;
    bfile.open("test_out2.bat",ios::out|ios::binary);
    cout<<"*** sizeof(buff)----"<<sizeof(buff)<<endl;
    cout<<"*** sizeof(buff2)----"<<sizeof(buff2)<<endl;
    //     memset(buff,0,sizeof(buff));
    memset(buff,0,50);
    cout<<"请输入："<<endl;
    cin>>buff;
    bfile.write(buff,50);
    //     bfile.flush();
    bfile.close();

    bfile.open("test_out2.bat",ios::in|ios::binary);
    while (!bfile.eof())
    {
      memset(buff,0,50);
      bfile.read(buff,50);
      if (bfile.tellg()>0)
      {
          cout<<buff;

      }
      

    }

    cout<<endl;
    bfile.close();

    //将test_out2.bat文件内容复制到test_out2.txt文件中
    ifstream inFile("test_out2.bat",ios::binary);
    ofstream outFile3("test_out2.txt",ios::app);
    char buff3[50];
    char c;

    for (size_t i = 0; i < 100; i++)
    {
     istream& ist = inFile.get(c);
     bool flags = ist.operator bool();
     cout<<"*** ist="<< flags <<endl;
     // std::clog << "typeid(inFile.get(c)).name()---" <<i<<"--"<<typeid(inFile.get(c)).name()<< std::endl;
    }
    bool falg = -1;
    if (falg)
    {
     cout<<"*** falg="<<falg<<endl;
    }
    
    
    while (inFile.get(c))
    {
        cout<<"*** while c="<<c<<endl;
    }
    
    while (inFile.read(buff3,50))
    {
      outFile3.write(buff3,50);
    }
    inFile.close();
    outFile3.close();

    vector<int> v1;
    vector<int> v2 {v1};//c++14 -style copy construction

    return 0;

}